﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using System.Threading;
using System.Data.SqlClient;
using System.Configuration;

namespace MyCommerce.Windows.GeneralParameters.Views
{
    public partial class BackUpView : BaseForm
    {
        public BackUpView()
        {
            InitializeComponent();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            if (Context.ShowQuestionMessageBox("¿Seguro que desea generar el respaldo?", "IMPORTANTE") == System.Windows.Forms.DialogResult.Yes)
            {
                lblTotalProducts.Visible = true;
                pictureLoad.Visible = true;
                Thread backTest = new Thread(
                    delegate(object state)
                    {
                        UpdatesStoresProcedures();
                    });

                backTest.Start();
                lblTotalProducts.Visible = false;
                pictureLoad.Visible = false;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdatesStoresProcedures()
        {
            string cnnString = ConfigurationManager.ConnectionStrings["MyCommerceEntities"].ConnectionString;

            string[] contections = cnnString.Split(new string[] { @"provider connection string=" }, StringSplitOptions.None);

            cnnString = "";
            for (int index = 1; index < contections.Length; index++)
            {
                cnnString = contections[index];
            }

            cnnString = cnnString.Replace(@"""", "");

            ///Buscamos en la BD todos comandos que no estan en el archivo XML
            using (SqlConnection vCnn = new SqlConnection(cnnString))
            {
                try
                {
                    //SqlDataAdapter daQuery = new SqlDataAdapter(queryToExecute, vCnn);
                    //daQuery.

                    SqlCommand cmd = new SqlCommand();
                    // Configuramos el comando
                    //
                    cmd.Connection = vCnn;
                    cmd.CommandText = "pBackUpProcess";
                    cmd.Parameters.AddWithValue("@TNombreBD", "MyCommerce");
                    cmd.Parameters.AddWithValue("@TDatabaseCompression", true);
                    cmd.Parameters.AddWithValue("@TWinrarCompression", false);
                    cmd.Parameters.AddWithValue("@TPath", @"C:\BackUp\");

                    // Indicamos que se trata de un procedimiento almacenado
                    //
                    cmd.CommandType = CommandType.StoredProcedure;
                    // Abrimos la conexión
                    //
                    vCnn.Open();
                    // Ejecutamos el comando
                    //
                    cmd.ExecuteNonQuery();

                    Context.ShowInformationMessageBox("El respaldo se generó correctamente.", "IMPORTANTE.");
                }
                catch (Exception ex)
                {
                    Context.HandleException(ex);
                }
            }
        }
    }
}
